package com.vast.vastlcd.lcdAPI;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 
 */
public class LCDSetPositionRequest {
	public LCDMessageHeader lcdMessageHeader;
	public int nX;
	public int nY;

	/**
	 * @return
	 */
	public byte[] javaToNative () {
	 	if (this == null) 
	 		return null;
	 	
	 	byte[] buffer = null;
	 	try {
			// write data to a byte array and then ask super to convert to pMedium
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			LCDDataOutputStream writeOut = new LCDDataOutputStream(out);
			
			writeOut.write(lcdMessageHeader.javaToNative());
			
			writeOut.writeLSBInt(nX);
			writeOut.writeLSBInt(nY);
			
			buffer = out.toByteArray();
			writeOut.close();			
			
		} catch (IOException e) {

		}
		return buffer;
	}
	
	/**
	 * @param buffer
	 * @return
	 */
	static public LCDSetPositionRequest nativeToJava(byte[] buffer){	
	 
 		if (buffer == null) 
 			return null;
 		
 		LCDSetPositionRequest spr = new LCDSetPositionRequest();
 		try {
 			ByteArrayInputStream in = new ByteArrayInputStream(buffer);
 			LCDDataInputStream readIn = new LCDDataInputStream(in); 			
 			 
 			spr.nX = readIn.readLSBInt();
 			spr.nY = readIn.readLSBInt();
 			
 			readIn.close();
 		} catch (IOException ex) {
 			return null;
 		}
 		
 		return spr; 		
	}
	 
	/**
	 * @param msgBodyBuffer
	 */
	static void processMessage(byte[] msgBodyBuffer) {
		LCDSetPositionRequest spr = nativeToJava(msgBodyBuffer);
		 
		spr.updateViewer();		
	}
	
	/**
	 * 
	 */
	void updateViewer() {
		// set the position of the window
//		if (PlatformUI.isWorkbenchRunning()) {
//				
//			IWorkbench wb = PlatformUI.getWorkbench();	
//			final Display display = wb.getDisplay();
//		
//			while (display.isDisposed())
//			{
//		 								
//			}
//			
//			display.syncExec (new Runnable () {
//				public void run () {
//					while (display.isDisposed())
//					{}
//					
//					IWorkbench wb = PlatformUI.getWorkbench();
//				
//					IWorkbenchWindow workbenchWindow = wb.getActiveWorkbenchWindow(); 
//					if (workbenchWindow == null)
//						return;
//					
//					Shell shell = workbenchWindow.getShell();
//				
//					// Need to keep the size of the current shell
//					Rectangle shellRect = shell.getBounds();
//					
//					shell.setBounds(nX, nY, shellRect.width, shellRect.height);					
//				}
//			});										
//		}
 	}
}
